<template>
  <div class="addPurchaseContent">
    <div class="addPurchase">
      <div class="carefulText rowFlex columnCenter">
        <div class="icon">
          <i class="el-icon-warning"></i>
        </div>
        <span>请在此处新建拼团后，复制道具ID，填入微信小程序后台对应的道具ID中</span>
      </div>
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-position="top">
        <el-form-item label="拼团短剧" prop="playlet_id">
          <selectPlaylet
            type="playlet"
            :default-value="formData.playlet_id"
            @result="playletSelect"
          />
        </el-form-item>
        <el-form-item label="拼单价" prop="price">
          <el-input
            v-model="formData.price"
            clearable
            placeholder="请输入拼单价"
            oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"
          ></el-input>
        </el-form-item>
        <el-form-item label="售价" prop="origin_price">
          <el-input
            v-model="formData.origin_price"
            clearable
            placeholder="请输入售价"
            oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="dialog-footer">
      <div style="float: right;">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确定</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { purchaseSave } from '@/api/active'
export default {
  props: {
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      loading: false,
      formData: {
        playlet_id: '',
        price: '',
        origin_price: ''
      },
      rules: {
        playlet_id: [
          { required: true, message: '请选择拼团短剧', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入拼单价', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (Number(value) === 0) {
              callback(new Error('拼单价不能为 0'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        origin_price: [
          { required: true, message: '请输入售价', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (Number(value) === 0) {
              callback(new Error('售价不能为 0'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (this.row && this.row.id) {
        const { id, playlet_id, price, origin_price } = this.row
        this.formData = {
          id,
          playlet_id: Number(playlet_id),
          price,
          origin_price
        }
      }
    },
    moneyInput() {
      this.formData.price = checkMoney(Number(this.formData.price))
    },
    moneyInput_origin() {
      this.formData.origin_price = checkMoney(Number(this.formData.origin_price))
    },
    playletSelect(value) {
      this.formData.playlet_id = value
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.purchaseSave()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.$emit('cancel')
    },
    purchaseSave() {
      if (Number(this.formData.price) > Number(this.formData.origin_price)) {
        this.$message.warning('拼单价不能大于售价')
        return
      }
      this.loading = true
      purchaseSave(this.formData).then(res => {
        if (res.status_code === 1) {
          this.$message.success(res.msg)
          this.close()
          this.$emit('ok')
        }
        this.loading = false
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.addPurchase {
  width: 100%;
  height: auto;
  padding: 20px;

}
.addPurchaseContent{
  position: relative;
   .dialog-footer{
  }
}
</style>